define(function (require,exports,module) {
    require("easyui");
    require("easyui-css");
    require('easyui-icon-css');
	var url = require('mod-url'),
        utils = require('lm-util'),
        dialog = require('mod-consult');
	exports.loadAreaSuppliers = function(){
	    $("#salers").html("正在寻找该地区商户，请稍后......");
	    $.getJSON(url.liveAjax+'?callback=?',{ 'action':'servicerSearch', 'aid':$('#area-field').val(),'mid':$("input[name=modeid]:checked").val() }, function(data){
		    var servicers = '';
		    if(data.length) {
			    for (var iKey in data) {
				    servicers += '<a href="'+data[iKey]['linkurl']+'">'+data[iKey]['company']+'</a>'
			    }
			    $("#salers").html("<ul>"+servicers+"</ul>");
		    } else {
			    $("#salers").html("<ul>"+'没有查询到相关服务商信息!'+"</ul>");
		    }
	    })

	};

	(function(){
		var keyword = $('#kw').val();
		if (keyword != "") {
			var adviserHtml = "<ul class='text-ellipsis'>"
			$.getJSON(url.liveAjax+'?callback=?', { 'action':'kwSearch', 'kw':keyword , 'mid': $("input[name=modeid]:checked").val()},  function(data){
				if (data.length > 0) {
					for (var p in data ){
						adviserHtml += "<li><a title='"+data[p]['title']+"' aid='"+data[p]['id']+"'>"+data[p]['title']+"</a></li> "
					}
					adviserHtml += "</ul>"
					$("#question").html(adviserHtml);

					var consultDialog = new dialog("#question");
					consultDialog.showAnswer();

				} else {
					$("#question").html("<p class=\"warning\">没有您需要的信息,请换关键词搜索或者更换左侧的分类进行搜索!</p>");
				}
			})
		}
	})();
	exports.getAnswersByKeyword = function(){
	    var keyword = $('#inputQuestion').val();
	    if (keyword === "") {
	        alert('请输入查询的关键词!');
	    } else {
            var adviserHtml = "<ul class='text-ellipsis'>"
            $.getJSON(url.liveAjax+'?callback=?', { 'action':'kwSearch', 'kw':keyword , 'mid': $("input[name=modeid]:checked").val()},  function(data){
                if (data.length > 0) {
                    for (var p in data ){
                        adviserHtml += "<li><a title='"+data[p]['title']+"' aid='"+data[p]['id']+"'>"+data[p]['title']+"</a></li> "
                    }
                    adviserHtml += "</ul>"
                    $("#question").html(adviserHtml);

                    var consultDialog = new dialog("#question");
                    consultDialog.showAnswer();

                } else {
                    $("#question").html("<p class=\"warning\">没有您需要的信息,请换关键词搜索或者更换左侧的分类进行搜索!</p>");
                }
            })
	    }
	};

	exports.showAnswer = function(aid, selEle) {
	    if (!aid) {
	        alert('信息传递错误, 请重新操作!');
	    } else {
	        $.post(url.liveAjax,{ 'action':'answer', 'aid':aid },function(data){
	            if (data) {
	                $( '#dialog_title').text($(selEle).text());
	                $( '#dialog_content').text(data);
                    $("#dialog").dialog('open');
	            }
	        })
	    }
	}

    $("#submitQuestion").click(function(event){
        exports.getAnswersByKeyword();
    });
    $('body').on('keydown', function(event){
        if(event.ctrlKey && event.keyCode==13) {
            exports.getAnswersByKeyword();
        }
    });
    $("#supplier").click(function(){
        exports.loadAreaSuppliers();
    });
});